package com.example.heathy_me.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.heathy_me.R;
import com.example.heathy_me.cardDetail.Edit;
import com.example.heathy_me.cardDetail.cardDatabaseHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class Card extends Fragment {
    private String[] title={"姓名","年龄","体重","身高","医疗状况","过敏反应","用药","血型"};
    private String[] list={"name","date","weight","height","medical","allergy","medicine","blood"};
    private String[] content={"","","","","","","",""};
    private List<Map<String,String>> list_map = new ArrayList<Map<String,String>>();
    private ListView cListView;
    private Button editButton;
    private cardDatabaseHelper dbHelper;
    private Button icu;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.card,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //急救
        icu = getActivity().findViewById(R.id.icu);
        icu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phoneNumber = "120";
                Intent dialIntent=new Intent();
                dialIntent.setAction(Intent.ACTION_DIAL);
                dialIntent.setData(Uri.parse("tel:"+phoneNumber));
                startActivity(dialIntent);

            }
        });



        dbHelper=new cardDatabaseHelper(getContext(),"Card.db",null,3);
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query("card",null,null,null,null,null,null);
        if(cursor.moveToLast()){
            for (int i=0;i<8;i++){
                content[i]=cursor.getString(cursor.getColumnIndex(list[i]));
            }
        }
        cursor.close();
        for(int i=0;i<8;i++){
            Map<String,String> items = new HashMap<String, String>(); //创建一个键值对的Map集合
            items.put("title", title[i]);
            items.put("content", content[i]);
            list_map.add(items);   //把这个存放好数据的Map集合放入到list中，这就完成类数据源的准备工作
        }

        cListView = (ListView) getActivity().findViewById(R.id.cardlistView);

        SimpleAdapter simpleAdapter=new SimpleAdapter(getActivity(),list_map,R.layout.card_items,new String[]{"title","content"},new int[]{R.id.items_title,R.id.items_content});
        cListView.setAdapter(simpleAdapter);
        cListView.setTextFilterEnabled(true);
        cListView.setVisibility(View.VISIBLE);

        editButton=(Button)getActivity().findViewById(R.id.cardEdit);
        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), Edit.class);
                startActivity(intent);
            }
        });

    }


}
